package com.ssyc.gsk_master.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.android.arouter.utils.TextUtils;
import com.makeramen.roundedimageview.RoundedImageView;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.base.LazyBaseFragment;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.MathUtil;
import com.ssyc.common.utils.UiUtils;
import com.ssyc.gsk_master.R;
import com.ssyc.gsk_master.bean.BaseMsgInfo;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 基本信息
 * Created by Administrator on 2018/7/17 0017.
 */

public class MasterBaseMessageFragment extends LazyBaseFragment {

    private View view;
    private RoundedImageView ivHead;
    /**
     * 张三
     */
    private TextView tvName;
    /**
     * 女
     */
    private TextView tvSex;
    /**
     * 1992-10-15
     */
    private TextView tvBirthday;
    /**
     * 智爱天下教育百步亭校区
     */
    private TextView tvSchool;
    /**
     * 30/35
     */
    private TextView tvPayPersent;
    /**
     * 86%
     */
    private TextView tvTurn;
    /**
     * 86
     */
    private TextView tvScore;
    private ScrollView slContent;

    private RelativeLayout rlLoading;

    //老师账号和角色
    private String c, role;


    //单例模式
    private static class LazyHolder {
        private static final MasterBaseMessageFragment INSTANCE = new MasterBaseMessageFragment();
    }

    public static final MasterBaseMessageFragment newInstance(String c, String role) {
        MasterBaseMessageFragment masterBaseMessageFragment = LazyHolder.INSTANCE;
        Bundle bundle = new Bundle();
        bundle.putString("c", c);
        bundle.putString("role", role);
        masterBaseMessageFragment.setArguments(bundle);
        return masterBaseMessageFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.master_fragment_base_msg;
    }

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {

        showContent();

        //初始化视图
        initView(view);

        //获取老师账号和角色
        initIntent();

        //请求服务器获取数据
        http();
    }


    /**
     * 初始化视图
     *
     * @param view
     */
    public void initView(View view) {
        ivHead = view.findViewById(R.id.iv_head);
        tvName = view.findViewById(R.id.tv_name);
        tvSex = view.findViewById(R.id.tv_sex);
        tvBirthday = view.findViewById(R.id.tv_birthday);
        tvSchool = view.findViewById(R.id.tv_school);
        tvPayPersent = view.findViewById(R.id.tv_pay_persent);
        tvTurn = view.findViewById(R.id.tv_turn);
        tvScore = view.findViewById(R.id.tv_score);
        slContent = view.findViewById(R.id.sl_content);
        rlLoading = view.findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
        slContent.setVisibility(View.GONE);
    }

    /**
     * 获取老师账号和角色
     */
    private void initIntent() {
        role = getArguments().getString("role");
        c = getArguments().getString("c");
    }


    /**
     * 请求服务器获取数据
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "28");
        map.put("c", c);
        map.put("role", role);
        HttpUtils.post(HttpAdress.MASTERBASEMSG, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                if (slContent != null && slContent.getVisibility() != View.VISIBLE) {
                    slContent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    Log.i("test", "返回:" + response);
                    BaseMsgInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, BaseMsgInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }
                    if (null != info) {
                        if ("1".equals(info.getState())) {
                            //返回成功
                            //设置页面数据
                            setData(info);
                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }
            }
        });
    }

    /**
     * 设置页面数据
     *
     * @param info
     */
    private void setData(BaseMsgInfo info) {
        //设置头像
        ImageUtil.displayImage(getContext(), ivHead, info.getIcon(), R.drawable.defaultimage);
        //设置姓名
        tvName.setText(info.getName());
        //设置性别
        if ("1".equals(info.getSex())) {
            tvSex.setText("男");
        } else {
            tvSex.setText("女");
        }
        //设置生日
        tvBirthday.setText(info.getBirthday());

        //设置缴费比列
        tvPayPersent.setText(info.getPayment());

        //设置校区
        tvSchool.setText(info.getSchool());

        //设置平均出勤数
        Double attendance = Double.parseDouble(info.getAverageAttendance());

        //将返回的0.9转换为整数显示
        NumberFormat nt = NumberFormat.getPercentInstance();

        //设置百分数精确度1即保留1位小数
        nt.setMinimumFractionDigits(1);

        tvTurn.setText(nt.format(attendance));


        //设置平均成绩
        Double score = Double.parseDouble(info.getAverageExamResult());
        tvScore.setText(MathUtil.one(score) + "");

    }


}
